﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Manage
{
    public partial class Public_Ulevel : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_JType> JTypeDic = new Dictionary<int, Jy123.Model.Public_JType>();
        private Dictionary<int, Jy123.Model.Public_Level> levelDic = new Dictionary<int, Jy123.Model.Public_Level>();
        private Dictionary<int, string> conditionDic = new Dictionary<int, string>();
        private List<Jy123.Model.Public_JType> listJType;
        private List<Jy123.Model.Public_Level> listLevel;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Public_ULevel.aspx");
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("JFather IS NOT NULL");
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            listJType = BLLJType.GetModelList(strWhere.ToString());
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            listLevel = BLLLevel.GetModelList(string.Empty);
            Jy123.BLL.ULevel_QueueName BLLQueueName = new Jy123.BLL.ULevel_QueueName();
            List<Jy123.Model.ULevel_QueueName> listQueueName = BLLQueueName.GetModelList(string.Empty);
            foreach (Jy123.Model.ULevel_QueueName queueName in listQueueName)
            {
                conditionDic.Add(queueName.ID, queueName.CName);
            }
            foreach (Jy123.Model.Public_JType type in listJType)
            {
                JTypeDic.Add(type.ID, type);
            }
            foreach (Jy123.Model.Public_Level level in listLevel)
            {
                levelDic.Add(level.ID, level);
            }

            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_ULevel BLLStarType = new Jy123.BLL.Public_ULevel();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_ULevel ULevel = new Jy123.Model.Public_ULevel();
                SetValue(ULevel);
                BLLStarType.Add(ULevel);
            }
            else
            {
                int id = int.Parse(idHiddenEdit.Value);
                Jy123.Model.Public_ULevel ULevel = BLLStarType.GetModel(id);
                SetValue(ULevel);
                BLLStarType.Update(ULevel);
            }
            LoadInitialValue();
            ClearPage();
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_ULevel ULevel = e.Item.DataItem as Jy123.Model.Public_ULevel;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = ULevel.ID.ToString();
                Label nameLabel = e.Item.FindControl("ULevelNameLabel") as Label;
                Label JTypeNameLabel = e.Item.FindControl("JTypeNameLabel") as Label;
                Label moneyLabel = e.Item.FindControl("moneyLabel") as Label;
                Label conditionLabel = e.Item.FindControl("conditionLabel") as Label;
                Label valueLabel = e.Item.FindControl("valuesLabel") as Label;
                if (ULevel.FLevel.HasValue)
                {
                    nameLabel.Text = levelDic[ULevel.FLevel.Value].LName;
                }
                if (ULevel.FJType.HasValue)
                {
                    JTypeNameLabel.Text = JTypeDic[ULevel.FJType.Value].JName;
                }
                if (ULevel.MoneyFactor.HasValue)
                {
                    moneyLabel.Text = (Convert.ToInt32(ULevel.MoneyFactor.Value)).ToString();
                }
                string[] strCondition = ULevel.ClubCondition.Split();
                string strForShow = string.Empty;
                for (int i = 0; i < strCondition.Length; i++)
                {
                    strForShow += conditionDic[int.Parse(strCondition[i].Replace(",", ""))] + ",";
                }
                conditionLabel.Text = strForShow.Remove(strForShow.LastIndexOf(","));
                string strValue = ULevel.ClubConditionValues;
                valueLabel.Text = strValue.Remove(strValue.LastIndexOf(",")).Remove(0, 1);
            }
        }

        protected void ULevelOptionRepeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.ULevel_QueueName queueName = e.Item.DataItem as Jy123.Model.ULevel_QueueName;
                HiddenField idHidden = e.Item.FindControl("idHiddenCondition") as HiddenField;
                idHidden.Value = queueName.ID.ToString();
                CheckBox nameCheckBox = e.Item.FindControl("cNameCheckBox") as CheckBox;
                nameCheckBox.Text = queueName.CName;
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_ULevel BLLULevel = new Jy123.BLL.Public_ULevel();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_ULevel ULevel = BLLULevel.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHiddenEdit.Value = id.ToString();

                if (ULevel.FJType.HasValue)
                {
                    JTypeDropDown.SelectedValue = ULevel.FJType.Value.ToString();
                    JTypeDropDown_Change(JTypeDropDown, EventArgs.Empty);
                }
                if (ULevel.FLevel.HasValue)
                {
                    levelDropDown.SelectedValue = ULevel.FLevel.Value.ToString();
                }
                if (ULevel.MoneyFactor.HasValue)
                {
                    moneyTextBox.Text = (Convert.ToInt32(ULevel.MoneyFactor.Value)).ToString();
                }

                string[] condition = ULevel.ClubCondition.Split(',');
                string[] values = ULevel.ClubConditionValues.Split(',');
                for (int j = 0; j < ULevelOptionRepeater.Items.Count; j++)
                {
                    RepeaterItem optionItem = ULevelOptionRepeater.Items[j];
                    CheckBox checkBox = optionItem.FindControl("cNameCheckBox") as CheckBox;
                    TextBox textBox = optionItem.FindControl("valueTextBox") as TextBox;
                    HiddenField idHidden = optionItem.FindControl("idHiddenCondition") as HiddenField;
                    RequiredFieldValidator required = optionItem.FindControl("valueRequired") as RequiredFieldValidator;

                    for (int i = 0; i < condition.Length; i++)
                    {
                        if (condition[i].Equals(idHidden.Value))
                        {
                            checkBox.Checked = true;
                            textBox.Text = values[i];
                        }
                    }
                    textBox.Enabled = checkBox.Checked;
                    required.Enabled = checkBox.Checked;
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLULevel.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void JTypeDropDown_Change(object sender, EventArgs e)
        {
            int id = int.Parse(JTypeDropDown.SelectedValue);
            if (id > 0)
            {
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                string strWhere = "FJType = " + id.ToString();
                List<Jy123.Model.Public_Level> currentListLevel = BLLLevel.GetModelList(strWhere);
                levelDropDown.Items.Clear();
                levelDropDown.DataSource = currentListLevel;
                levelDropDown.DataTextField = "LName";
                levelDropDown.DataValueField = "ID";
                levelDropDown.DataBind();
                levelDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
                for (int j = 0; j < ULevelOptionRepeater.Items.Count; j++)
                {
                    RepeaterItem optionItem = ULevelOptionRepeater.Items[j];
                    CheckBox checkBox = optionItem.FindControl("cNameCheckBox") as CheckBox;
                    TextBox textBox = optionItem.FindControl("valueTextBox") as TextBox;                    
                    RequiredFieldValidator required = optionItem.FindControl("valueRequired") as RequiredFieldValidator;                    
                    textBox.Enabled = checkBox.Checked;
                    required.Enabled = checkBox.Checked;
                }
            }
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_ULevel BLLULevel = new Jy123.BLL.Public_ULevel();
            List<Jy123.Model.Public_ULevel> listULevel = BLLULevel.GetModelList(string.Empty);
            ULevelRepeater.DataSource = listULevel;
            ULevelRepeater.DataBind();
            ULevelRepeater.Visible = listULevel.Count > 0;
            

            Jy123.BLL.ULevel_QueueName BLLUName = new Jy123.BLL.ULevel_QueueName();
            List<Jy123.Model.ULevel_QueueName> listQueueName = BLLUName.GetModelList(string.Empty);       
            ULevelOptionRepeater.DataSource = listQueueName;
            ULevelOptionRepeater.DataBind();
            ULevelOptionRepeater.Visible = listQueueName.Count > 0;
            BindDropDown();            
        }

        private void BindDropDown()
        {
            JTypeDropDown.Items.Clear();
            JTypeDropDown.DataSource = listJType;
            JTypeDropDown.DataTextField = "JName";
            JTypeDropDown.DataValueField = "ID";
            JTypeDropDown.DataBind();
            JTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
            levelDropDown.Items.Clear();
            levelDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            moneyTextBox.Text = string.Empty;
        }

        private void SetValue(Jy123.Model.Public_ULevel ULevel)
        {
            if (int.Parse(levelDropDown.SelectedValue) > 0)
            {
                ULevel.FLevel = int.Parse(levelDropDown.SelectedValue);
            }
            if (int.Parse(JTypeDropDown.SelectedValue) > 0)
            {
                ULevel.FJType = int.Parse(JTypeDropDown.SelectedValue);
            }
            if (!string.IsNullOrEmpty(moneyTextBox.Text))
            {
                ULevel.MoneyFactor = decimal.Parse(moneyTextBox.Text);
            }
            string strCondition = ",";
            string strValues = ",";
            for (int j = 0; j < ULevelOptionRepeater.Items.Count; j++)
            {
                RepeaterItem item = ULevelOptionRepeater.Items[j];
                CheckBox checkBox = item.FindControl("cNameCheckBox") as CheckBox;
                TextBox textBox = item.FindControl("valueTextBox") as TextBox;
                HiddenField idHidden = item.FindControl("idHiddenCondition") as HiddenField;
                int optionId = int.Parse(idHidden.Value);
                if (checkBox.Checked)
                {
                    strCondition += idHidden.Value + ",";
                    strValues += textBox.Text + ",";
                }
            }
            if (strCondition.Length > 1)
            {
                ULevel.ClubCondition = strCondition;
                ULevel.ClubConditionValues = strValues;
            }
        }
    }
}
